package kr.hkit.powerjava.project_cafe;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;

public class ConnectionFactory {
	//연결 정보들 가지고 연결하기
	private String driver;  
	private String url;
	private String user;
	private String password;
	private static ConnectionFactory instance = new ConnectionFactory();
	
	public static Connection getConnection() {
		return instance.createConnection();
		
	}
	
	
	public ConnectionFactory() {
		loadProperties();
		try {
			Class.forName(driver); //lib에 있는 드라이버 클래스???
		} catch (ClassNotFoundException e) {
			System.err.printf("%s\n", "MySQL Driver not Found");
			System.exit(-1);
		}
	}

	
	private void loadProperties() {
		Properties prop = DBProperties.loadProperties("dbcon1.xml");
		driver = prop.getProperty("DRIVER");
		url = prop.getProperty("URL");
		user = prop.getProperty("USER");
		password = prop.getProperty("PASSWORD");
		
	}
	
	private Connection createConnection () {
		Connection con = null;
		try {
		con = DriverManager.getConnection(url, user, password);
		System.out.println("MySQL Connection Success");
		} catch (SQLException e) {
			System.err.printf("%s", "ERROR : Unable Connect to MySQL");
		}
		
		return con;
		
	}
}
